package com.example.java_chatroom.api;

import com.example.java_chatroom.model.Friend;
import com.example.java_chatroom.model.FriendMapper;
import com.example.java_chatroom.model.User;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
public class FriendApi {
    @Resource
    private FriendMapper friendMapper;

    @GetMapping("/friendList")
    @ResponseBody
    public Object getFriendList(HttpServletRequest req) {
        // 1.从会话中调查 userID
        HttpSession session = req.getSession(false);
        if(session == null){
            log.info("session is null");
            return new ArrayList<Friend>();
        }

        User user = (User) session.getAttribute("user");
        if(user == null){
            log.error("user is null");
            return new ArrayList<Friend>();
        }

        List<Friend> friendList = friendMapper.selectFriendList(user.getUserId());
        return friendList;
        }
}
